import { defineStore, createPinia } from 'pinia';
// 持久化插件
import piniaPluginPersist from 'pinia-plugin-persist';

// 定义导出容器
export const useMainStore = defineStore('main', {
	state: () => {
		return {
			appid: 'wxcbd7811446b1a759',
			redirect_uri: 'https://ztcy.huimaibuy.com/h5',
			userInfo: null as any,
			storeInfo: null as any,
			parent_id: null as null | string | number,
			storeIsLogin: false,
			isUserDetailPath: false,
			isShare: false,
			cityInfo: null as any,
			wxConfig: null as any,
			homeTabActive: null as any,
			storesIntroduce: null as any,
			matchmakerIntroduce: null as any,
			promoteContents: null as any,
			shortVideoUrl: null as any,
		};
	},
	getters: {
		user_id(state) {
			return state.userInfo?.user_id || state.userInfo?.id;
		}
	},
	persist: {
		enabled: true,
		strategies: [
			{ key: 'userInfo', paths: ['userInfo'], storage: localStorage },
			{ key: 'storeInfo', paths: ['storeInfo'], storage: sessionStorage },
			{ key: 'parent_id', paths: ['parent_id'], storage: sessionStorage },
			{ key: 'storeIsLogin', paths: ['storeIsLogin'], storage: sessionStorage },
			{ key: 'isUserDetailPath', paths: ['isUserDetailPath'], storage: sessionStorage },
			{ key: 'isShare', paths: ['isShare'], storage: sessionStorage },
			{ key: 'cityInfo', paths: ['cityInfo'], storage: sessionStorage },
			{ key: 'wxConfig', paths: ['wxConfig'], storage: sessionStorage },
			{ key: 'homeTabActive', paths: ['homeTabActive'], storage: sessionStorage },
			{ key: 'storesIntroduce', paths: ['storesIntroduce'], storage: sessionStorage },
			{ key: 'matchmakerIntroduce', paths: ['matchmakerIntroduce'], storage: sessionStorage },
			{ key: 'promoteContents', paths: ['promoteContents'], storage: sessionStorage },
			{ key: 'shortVideoUrl', paths: ['shortVideoUrl'], storage: sessionStorage },
		]
	}
});

const pinia = createPinia().use(piniaPluginPersist);

export default pinia;
